package top.huhuiyu.myforum.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import top.huhuiyu.myforum.entity.TbAdmin;
import top.huhuiyu.myforum.model.TbAdminModel;
import top.huhuiyu.myforum.dao.TbAdminDAO;
import top.huhuiyu.myforum.util.JsonMessage;

/**
 * TbAdmin的控制器类
 */
@RestController
@RequestMapping("/tbAdmin")
public class TbAdminController {

	@Autowired
	private TbAdminDAO tbAdminDAO;

	@RequestMapping("/save")
	public JsonMessage save(TbAdminModel tbAdminModel) throws Exception {
		// 测试模板 /tbAdmin/save
		// {"tbAdmin.username":"1","tbAdmin.password":"2","tbAdmin.nickname":"3","tbAdmin.isEnable":"4","tbAdmin.created":"5"}
		tbAdminDAO.save(tbAdminModel.getTbAdmin());
		return JsonMessage.getSuccessMessage("数据保存成功");
	}

	@RequestMapping("/update")
	public JsonMessage update(TbAdminModel tbAdminModel) throws Exception {
		// 测试模板 /tbAdmin/update
		// {"tbAdmin.aid":"1","tbAdmin.username":"2","tbAdmin.password":"3","tbAdmin.nickname":"4","tbAdmin.isEnable":"5","tbAdmin.created":"6"}
		tbAdminDAO.update(tbAdminModel.getTbAdmin());
		return JsonMessage.getSuccessMessage("数据修改成功");
	}

	@RequestMapping("/delete")
	public JsonMessage delete(TbAdminModel tbAdminModel) throws Exception {
		// 测试模板 /tbAdmin/delete {"tbAdmin.aid":"1"}
		tbAdminDAO.delete(tbAdminModel.getTbAdmin());
		return JsonMessage.getSuccessMessage("数据删除成功");
	}

	@RequestMapping("/queryByKey")
	public JsonMessage queryByKey(TbAdminModel tbAdminModel) throws Exception {
		// 测试模板 /tbAdmin/queryByKey {"tbAdmin.aid":"1"}
		TbAdmin tbAdmin = tbAdminDAO.queryByKey(tbAdminModel.getTbAdmin());
		JsonMessage message = JsonMessage.getSuccessMessage("数据查询完成");
		message.putData("tbAdmin", tbAdmin);
		return message;
	}

	@RequestMapping("/queryAll")
	public JsonMessage queryAll(TbAdminModel tbAdminModel) throws Exception {
		// 测试模板 /tbAdmin/queryAll {}
		PageHelper.startPage(tbAdminModel.getPage().getPageNumber(), tbAdminModel.getPage().getPageSize());
		Page<TbAdmin> list = (Page<TbAdmin>) tbAdminDAO.queryAll();
		tbAdminModel.getPage().setPageInfo(list);
		JsonMessage message = JsonMessage.getSuccessMessage("查询成功！");
		message.putData("list", list);
		message.putData("page", tbAdminModel.getPage());
		return message;
	}

	@RequestMapping("/login")
	public JsonMessage login(TbAdminModel tbAdminModel) throws Exception {
		// 测试模板 /tbAdmin/login
		// {"tbAdmin.username":"admin","tbAdmin.password":"manager"}
		TbAdmin tbAdmin = tbAdminDAO.login(tbAdminModel.getTbAdmin());
		if (tbAdmin == null) {
			return JsonMessage.getFailMessage("用户登录失败");
		}
		tbAdmin.setPassword("****************");
		JsonMessage message = JsonMessage.getSuccessMessage("登陆成功！");
		message.putData("tbAdmin", tbAdmin);
		return message;
	}

}
